<?php
/**
 * --------------------------------------------------
 * Copyright © 2021 成都拉风网络 版权所有
 *
 * 功能描述：
 *
 * 创建标识：Created by ZhongYuChan On 2021/4/15 14:59
 *
 * 修改标识：
 * 修改描述：
 *---------------------------------------------------
 */

namespace App\Common;


use Monolog\Handler\StreamHandler;
use Monolog\Logger;

class Monolog
{
    protected $config = [
        "title" =>"log",
        "channel" => 'debug',
        "file_root"=> 'runtime/log/'
    ];

    /**
     * @var Logger
     */
    protected $logger;

    protected static $instance;

    public static function getInstance(){
        if (!self::$instance){
            self::$instance = new self();
        }
        return self::$instance;
    }


    /**
     * @return Logger
     */
    protected function getLogger(){
        if(!$this->logger){
            $this->logger = new Logger('logger');
            $this->getLogger()->pushHandler(new StreamHandler($this->getLogRoot(), Logger::INFO));
        }
        return $this->logger;
    }

    public function getLogRoot(){
        return dirname(dirname(__DIR__)).'/runtime/'.date("Ymd").'/'.date('H').'.log';
    }

    /**
     * @param $msg
     * @param array $data
     */
    public function debug($msg,$data = []){
        $this->getLogger()->info($msg.'. '.json_encode($data));
    }
}